<?php
namespace Home\Model;

use Think\Model;
class NavModel extends Model{

    public function getTree() {

        //得到所有的节点
        $object = $this->field('id,text,url,iconCls,nid')->select();

        //声明数组
        $tree = array();

        //将所有节点筛选出来
        foreach ($object as $key => $value) {
            if ($value['nid'] == 0) {
                $tree[] = $value;
            }
        }

        //将所有的子节点合并到对应的主节点中
        foreach ($tree as $tressKey => $treeValue) {
            foreach ($object as $objectKey => $objectValue) {
                if ($treeValue['id'] == $objectValue['nid']) {
                    $tree[$tressKey]['children'][] = $objectValue;
                }
            }
        }
        return $tree;

    }
}
